package com.mida.bms.report.service.impl;

import com.mida.bms.report.mapper.DocReportRequisitionRelationMapper;
import com.mida.bms.report.service.DocReportRequisitionRelationService;
import com.mida.bms.report.vo.*;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;

@Service
@Slf4j
@Transactional
public class DocReportRequisitionRelationServiceImpl implements DocReportRequisitionRelationService {

    @Autowired
    private DocReportRequisitionRelationMapper docReportRequisitionRelationMapper;

    /**
     * 获取报告状态
     * @param requestSoid 申请单soid
     * @return 报告的流转状态
     */
    @Override
    public ListResult<DocReportRequisitionRelationVo> selectStautusByRequestSoid(Long requestSoid) {
        ListResult result = new ListResult();
        DocReportRequisitionRelationVo docReportRequisitionRelationVo = null;
        if (requestSoid != null) {
            docReportRequisitionRelationVo = docReportRequisitionRelationMapper.selectByRequestSoid1(requestSoid);
        }
        buildListSuccess(result, docReportRequisitionRelationVo);
        return result;
    }

    /**
     * 获取书写医生或审核医生标识
     * @param requestSoid 申请单soid
     * @return 书写医生/审核医生
     */
    @Override
    public ListResult<DocReportRequisitionRelationVo> selectDoctorByRequestSoid(Long requestSoid) {
        ListResult result = new ListResult();
        DocReportRequisitionRelationVo docReportRequisitionRelationDoctorVo = null;
        if (requestSoid != null ) {
            docReportRequisitionRelationDoctorVo = docReportRequisitionRelationMapper.selectDoctorByRequestSoid(requestSoid);
        }
        buildListSuccess(result, docReportRequisitionRelationDoctorVo);
        return result;
    }

    /**
     * 、分页查询待审核/已完成状态下报告list
     * @param vo reportProcessStatus -->16033111859003392 书写完成待审核  16033111879974912 审核完成
     * @return 申请单soid
     */
    @Override
    public ListResult<PageResultVo<DocReportListVo>> findReportPage(DocReportListPageVo vo) {
        ListResult result = new ListResult();
        PageResultVo pvo = new PageResultVo();

        Page page = new Page(vo.getCurrentPage(), vo.getPageSize());
        pvo.setPage(page);
        List<DocReportListVo> list = null;
        DocReportListPage docReportListPage = DocReportListPage
                .builder()
                .pageBegin((vo.getCurrentPage() - 1) * vo.getPageSize())
                .pageEnd(vo.getCurrentPage() * vo.getPageSize())
                .reportProcessStatus(vo.getReportProcessStatus())
                .build();

        int count = docReportRequisitionRelationMapper.findPageCount(docReportListPage);
        if (count > 0) {
            page.setTotalCount(count);
            list =  docReportRequisitionRelationMapper.findReportPage(docReportListPage);
        } else {
            page.setTotalCount(count);
            list = new ArrayList<DocReportListVo>(0);
        }
        pvo.setList(list);
        buildListSuccess(result, pvo);

        return result;
    }

    @Override
    public ListResult<DocReportRequisitionRelationVo> selectDoctorAndStatus(ReportSearchRequest reportSearchRequest) {
        ListResult result = new ListResult();
        DocReportRequisitionRelationVo docReportRequisitionRelationDoctorVo = null;
        if (ObjectUtils.isNotEmpty(reportSearchRequest)) {
            docReportRequisitionRelationDoctorVo = docReportRequisitionRelationMapper.selectDoctorAndStatus(reportSearchRequest);
        }
        buildListSuccess(result, docReportRequisitionRelationDoctorVo);
        return result;
    }

    @Override
    public ListResult<List<DocReportRequisitionRelationVo>> selectDoctorAndStatusList(ReportSearchVo reportSearchVo) {
        ListResult result = new ListResult();
        List<DocReportRequisitionRelationVo> docReportRequisitionRelationVoList = null;
        List<ReportSearchRequest> reportSearchRequestList = reportSearchVo.getReportSearchRequestList();
        if (ObjectUtils.isNotEmpty(reportSearchRequestList)) {
            docReportRequisitionRelationVoList = docReportRequisitionRelationMapper.selectDoctorAndStatusList(reportSearchRequestList);
        }
        buildListSuccess(result, docReportRequisitionRelationVoList);
        return result;
    }
}
